package com.linkhub.oms.admin.modules.supplier.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import com.linkhub.oms.admin.modules.satoken.session.LhSessionContext;
import com.linkhub.oms.admin.modules.supplier.domain.param.SupplierAddParam;
import com.linkhub.oms.admin.modules.supplier.domain.vo.SupplierVO;
import com.linkhub.oms.admin.modules.supplier.service.SupplierBizService;
import com.linkhub.oms.common.constant.RoleConstant;
import com.linkhub.oms.common.response.DataResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Description: SupplierController
 * @Author: echo
 * @Date: 2024/4/24 17:08
 * @Version: 1.0
 */
@Slf4j
@RestController
@Api(tags = {"供应商管理模块"})
@RequestMapping("/supplier")
@SaCheckLogin
public class SupplierController {

    @Resource
    private SupplierBizService supplierBizService;

    @GetMapping("/list")
    @ApiOperation(value = "查询供应商列表接口")
    @SaCheckRole(value = {RoleConstant.CUSTOMER})
    public DataResponse<List<SupplierVO>> listSupplier() {
        return DataResponse.success(supplierBizService.querySuppliers(LhSessionContext.getUserId()));
    }


    @PostMapping
    @ApiOperation(value = "新增供应商接口")
    @SaCheckRole(value = {RoleConstant.CUSTOMER})
    public DataResponse<Void> save(@Valid @RequestBody SupplierAddParam param) {
        supplierBizService.save(param);
        return DataResponse.success();
    }


    @PutMapping("/{sysSupplierId}")
    @ApiOperation(value = "编辑供应商接口")
    @SaCheckRole(value = {RoleConstant.CUSTOMER})
    public DataResponse<Void> edit(@PathVariable("sysSupplierId") String sysSupplierId,
                                   @Valid @RequestBody SupplierAddParam param) {

        supplierBizService.edit(sysSupplierId, param);

        return DataResponse.success();
    }


    @GetMapping("/list/{userId}")
    @ApiOperation(value = "查询客户的供应商列表接口")
    @SaCheckRole(value = {RoleConstant.EMPLOYEE, RoleConstant.STORE_STAFF}, mode = SaMode.OR)
    public DataResponse<List<SupplierVO>> listSupplier(@PathVariable("userId") String userId) {
        return DataResponse.success(supplierBizService.querySuppliers(userId));
    }

}
